/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.txt.access.capability.finder.util;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 *
 * @author seccia
 */
public class ResourceIDRegexCreator {

    //public static final String defaultPath = "D:/Developing/ResourceIDMatch.txt";
    public static final String REGEX_SLASH = "\\";
    public static final String SPECIAL_CHARS = ""
            + "[" + REGEX_SLASH + "(" + REGEX_SLASH + "["
            + REGEX_SLASH + "{" + REGEX_SLASH + "\\"
            + REGEX_SLASH + "^" + REGEX_SLASH + "-"
            + REGEX_SLASH + "$" + REGEX_SLASH + "|"
            + REGEX_SLASH + "]" + REGEX_SLASH + "}"
            + REGEX_SLASH + ")" + REGEX_SLASH + "?"
            + REGEX_SLASH + "+" + REGEX_SLASH + "." + "]";
    public static final String REGEX_DOT = "\\.";
    private static final String REGEX_WILD = "[#\\*]";
    public static final String REGEX_CHARS = "([\\w\\-\\s_]+)";
    public static final String REGEX_APPEND = ""
            + "(" + REGEX_DOT + "(" + REGEX_CHARS + "|" + REGEX_WILD + "))*";
    public static final String REGEX_RESOURCE_ID = ""
            + "(" + REGEX_CHARS + "|" + REGEX_WILD + ")"
            + REGEX_APPEND;

    /*public static void main(String[] args) throws Exception {

        ArrayList<MatchTest> mts = MatchTestHelper.readMatchTestFromFile(defaultPath);

        System.out.println("***********************************");
        for (MatchTest mt : mts) {
            String topic = mt.getTopic();
            String resourceID = mt.getResourceID();
            System.out.println("Macht Test");
            System.out.println(". Resource ID: \"" + resourceID + "\"");
            System.out.println(". Topic: \"" + topic + "\"");
            String resourceIDRegex = recoverResourceIDPatternRegex(resourceID);
            System.out.println(". ResourceID Regex: \"" + resourceIDRegex + "\"");
            Pattern finalPattern = Pattern.compile(resourceIDRegex, Pattern.CASE_INSENSITIVE);
            Matcher resourceIDMatcher = finalPattern.matcher(resourceID);
            if (resourceIDMatcher.matches()) {
                System.out.println(". ResourceID Match YES.");
            } else {
                System.out.println(". ResourceID Match NO.");
            }
            Matcher topicMatcher = finalPattern.matcher(topic);
            if (topicMatcher.matches()) {
                System.out.println(". Topic Match YES.");
            } else {
                System.out.println(". Topic Match NO.");
            }
            System.out.println("***********************************");
        }
    }*/

    public static String recoverResourceIDPatternRegex(String resourceID) {
        String[] items = resourceID.split("\\.");
        //The start of the regular expression
        String resourceIDRegex = "^";
        //The dots / nodes counter
        int dots = 0;
        //Useful to find any special chars within the resourceID string.
        Pattern specialCharsPattern = Pattern.compile(SPECIAL_CHARS);
        //appen or not the last pattern regex section.
        boolean escape = false;
        //For each item / node in the resource ID / tree
        for (String item : items) {
            //In this case the char "*" means a node in the tree, it
            //can be replaced by the regex REGEX_CHARS.
            if (item.equals("*")) {
                resourceIDRegex += REGEX_CHARS;
                //Escape the REGEX_APPEND if the "*" is the last item.
                if (dots == items.length - 1) {
                    escape = true;
                }
            } //In this case the char "#" means one or more nodes in the tree,
            //it can be replaced by the regex REGEX_RESOURCE_ID.
            else if (item.equals("#")) {
                resourceIDRegex += REGEX_RESOURCE_ID;
                //Escape the REGEX_APPEND if the "#" is the last item.
                if (dots == items.length - 1) {
                    escape = true;
                }
            } else {
                //Find any special character in the current item.
                //replace by adding the escape char "\" in order to
                //be part of a valid regular expression.
                Matcher matcher = specialCharsPattern.matcher(item);
                while (matcher.find()) {
                    String special = matcher.group();
                    item = item.replace(special, REGEX_SLASH + special);
                }
                resourceIDRegex += item;
            }
            //Append the char "." to the regex
            if (dots != items.length - 1) {
                resourceIDRegex += REGEX_DOT;
            }
            dots++;
        }
        //If the current item is not a "*" or a "#" then append
        //to the regex REGEX_APPEND
        if (!escape) {
            resourceIDRegex += REGEX_APPEND;
        }
        //The end of the regex.
        resourceIDRegex += "$";
        return resourceIDRegex;
    }
}
